package com.arpa.wly.waybill;


import cn.hutool.json.JSONUtil;
import com.arpa.wms.domain.entity.Delivery;
import com.qimen.api.request.DeliveryorderConfirmRequest;
import com.taobao.pac.sdk.cp.PacClient;
import com.taobao.pac.sdk.cp.SendSysParams;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_DISCARD.TmsWaybillDiscardRequest;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_GET.*;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_SUBSCRIPTION_QUERY.TmsWaybillSubscriptionQueryRequest;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_UPDATE.Recipient;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_UPDATE.TmsWaybillUpdateRequest;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_DISCARD.TmsWaybillDiscardResponse;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_GET.TmsWaybillGetResponse;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_GET.WaybillCloudPrintResponse;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_SUBSCRIPTION_QUERY.TmsWaybillSubscriptionQueryResponse;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_UPDATE.TmsWaybillUpdateResponse;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


class TmsWayBill {

    /**
     * 测试环境请求url
     */


    private static final String DAILY_URL = "https://linkdaily.tbsandbox.com/gateway/link.do";

    /**
     * 测试环境appKey
     */


    private static final String DAILY_APP_KEY = "766654";

    /**
     * 测试环境secretKey
     */


    private static final String DAILY_SECRET_KEY = "F53eqq903jQySV100Z8w06f9g914A13Z";

    /**
     * 测试环境token，token是商家在物流云平台授权ISV后生成的授权码，目前需要商家配置在ISV的软件中
     */


    private static final String DAILY_TOKEN = "TmpFU1ZOUGoyRnoybDZmT3lyaW9hU3E4SDlobjdvMlJkemsxaGhHaVFMa2ZpMWtwOWsxSjFIUmMrUTlmNWdHVQ==";


    public void tmsWaybillSubscriptionQuery() {
        PacClient client = new PacClient(DAILY_APP_KEY, DAILY_SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(DAILY_TOKEN);

        TmsWaybillSubscriptionQueryRequest request = new TmsWaybillSubscriptionQueryRequest();
        TmsWaybillSubscriptionQueryResponse tmsWaybillSubscriptionQueryResponse = client.send(request, params);
        if (!tmsWaybillSubscriptionQueryResponse.isSuccess()) {
            return;
        }
    }

    @Test
    public void tmsWaybillGet() {
        PacClient client = new PacClient(DAILY_APP_KEY, DAILY_SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(DAILY_TOKEN);
        TmsWaybillGetRequest request = new TmsWaybillGetRequest();
        request.setNeedEncrypt(true);
        // 配送公司编码
        request.setCpCode("EMS");
        // 发件人信息
        UserInfoDto sender = new UserInfoDto();
        request.setSender(sender);
        // 发件人姓名
        sender.setName("收件人");
        // 发件人手机号
        sender.setMobile("13365496933");
        // 寄件地址，这里的地址需要是卖家订购电子面单服务时使用的订购地址，具体可以通过TMS_WAYBILL_SUBSCRIPTION_QUERY接口获取
        AddressDto sendAddress = new AddressDto();
        sender.setAddress(sendAddress);
        sendAddress.setProvince("山东省");
        sendAddress.setCity("临沂市");
        sendAddress.setDistrict("兰山区");
        sendAddress.setDetail("应用科学城");

        // 收件信息
        ArrayList<TradeOrderInfoDto> tradeOrderInfoDtos = new ArrayList<>();
        request.setTradeOrderInfoDtos(tradeOrderInfoDtos);
        TradeOrderInfoDto tradeOrderInfoDto = new TradeOrderInfoDto();
        tradeOrderInfoDto.setObjectId("1111111");
        tradeOrderInfoDto.setTemplateUrl("http://cloudprint.cainiao.com/template/standard/345208/4");
        tradeOrderInfoDtos.add(tradeOrderInfoDto);
        OrderInfoDto orderInfoDto = new OrderInfoDto();
        tradeOrderInfoDto.setOrderInfo(orderInfoDto);

        orderInfoDto.setOrderChannelsType("TB");
        ArrayList<String> orderList = new ArrayList<>();
        orderInfoDto.setTradeOrderList(orderList);
        // 订单列表，这里的场景是一个订单获取一个面单号
        orderList.add("1123");

        PackageInfoDto packageInfoDto = new PackageInfoDto();
        tradeOrderInfoDto.setPackageInfo(packageInfoDto);
        ArrayList<Item> items = new ArrayList<>();
        packageInfoDto.setItems(items);

        Item item = new Item();
        item.setName("测试商品");
        item.setCount(1);
        items.add(item);


        UserInfoDto receiver = new UserInfoDto();
        tradeOrderInfoDto.setRecipient(receiver);
        receiver.setName("收件人");
        receiver.setMobile("18669656819");
        AddressDto receiveAddress = new AddressDto();
        receiver.setAddress(receiveAddress);

        receiveAddress.setProvince("山东省");
        receiveAddress.setCity("临沂市");
        receiveAddress.setDistrict("兰山区");
        receiveAddress.setDetail("应用科学城");

        TmsWaybillGetResponse response = client.send(request, params);
        if (!response.isSuccess()) {
            return;
        }
        List<WaybillCloudPrintResponse> waybillCloudPrintResponseList = response.getWaybillCloudPrintResponseList();
        for (WaybillCloudPrintResponse waybillCloudPrintResponse : waybillCloudPrintResponseList) {
            System.out.println(JSONUtil.toJsonStr(waybillCloudPrintResponse));
        }

    }

    public void tmsWaybillUpdate() {
        PacClient client = new PacClient(DAILY_APP_KEY, DAILY_SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(DAILY_TOKEN);

        //更新一下面单上的收件人手机号
        TmsWaybillUpdateRequest request = new TmsWaybillUpdateRequest();

        //配送公司编码
        request.setCpCode("EMS");
        //面单号
        request.setWaybillCode("1101469709602");

        Recipient recipient = new Recipient();
        recipient.setMobile("13100000000");
        request.setRecipient(recipient);
        TmsWaybillUpdateResponse tmsWaybillUpdateResponse = client.send(request, params);
        if (!tmsWaybillUpdateResponse.isSuccess()) {
            return;
        }
        tmsWaybillUpdateResponse.getPrintData();
    }

    public void tmsWaybillDiscard() {
        PacClient client = new PacClient(DAILY_APP_KEY, DAILY_SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(DAILY_TOKEN);

        TmsWaybillDiscardRequest request = new TmsWaybillDiscardRequest();
        //配送公司编码
        request.setCpCode("EMS");
        request.setWaybillCode("1101469709602");

        TmsWaybillDiscardResponse tmsWaybillDiscardResponse = client.send(request, params);
        if (!tmsWaybillDiscardResponse.isSuccess()) {
            return;
        }
    }

    private void deliveryConfirm(Delivery delivery) {
        DeliveryorderConfirmRequest req = new DeliveryorderConfirmRequest();
        req.setCustomerId("stub-cust-code");
        req.setVersion("2.0");
        DeliveryorderConfirmRequest.DeliveryOrder deliveryOrder = new DeliveryorderConfirmRequest.DeliveryOrder();
        // 出库单号
        deliveryOrder.setDeliveryOrderCode(delivery.getDeliveryOrderCode());
        // 仓库编码
        deliveryOrder.setWarehouseCode(delivery.getWarehouseCode());
        // 出库单类型
        deliveryOrder.setOrderType(delivery.getOrderType());
        // 包裹信息
        List<DeliveryorderConfirmRequest.Package> packages = new ArrayList<>();
        DeliveryorderConfirmRequest.Package package1 = new DeliveryorderConfirmRequest.Package();
        // 物流公司编码
        package1.setLogisticsCode(delivery.getLogisticsCode());
        // 运单号
        package1.setExpressCode(delivery.getWaybillCode());
        packages.add(package1);

        deliveryOrder.setPackages(packages);

        // 商品列表
        List<DeliveryorderConfirmRequest.Item> items = new ArrayList<>();
        DeliveryorderConfirmRequest.Item item = new DeliveryorderConfirmRequest.Item();
        // 商品编码
        item.setItemCode("1234");
        // 包裹内该商品数量
        item.setQuantity(BigDecimal.TEN.longValue());
        items.add(item);
        deliveryOrder.setItems(items);

        // 单据列表
        List<DeliveryorderConfirmRequest.OrderLine> orderLines = new ArrayList<>();
        DeliveryorderConfirmRequest.OrderLine orderLine = new DeliveryorderConfirmRequest.OrderLine();
        // 商品编码
        orderLine.setItemCode("1234");
        // 应发商品数量
        orderLine.setPlanQty("12");
        // 实发商品数量
        orderLine.setActualQty("12");
        orderLines.add(orderLine);

        req.setOrderLines(orderLines);

        // 创建json对象
        net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(req);

        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json.toString());

    }
}

